import { isEmpty } from './is';

/**
 * 评论
 * @param val 评论数量
 * @returns 评论
 */
export function comment(val: number | undefined) {
  let c = '';
  if (isEmpty(val)) {
    return '';
  }
  if (val >= 100000) {
    c = parseInt(val / 100000 + '') + '0万+条评论';
  } else if (val >= 10000) {
    c = parseInt(val / 10000 + '') + '万+条评论';
  } else if (val >= 1000) {
    c = parseInt(val / 1000 + '') + '000+条评论';
  } else if (val >= 100) {
    c = parseInt(val / 100 + '') + '00+条评论';
  } else if (val >= 10) {
    c = parseInt(val / 10 + '') + '0+条评论';
  } else {
    c = val + '条评论';
  }
  return c;
}

// 防抖函数
export const debounce = (fn: Function, delay = 300) => {
  let timer: ReturnType<typeof setTimeout>;
  return (...args: any[]) => {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
};
